<template>
  <view class="container">
    <!-- <view class="bg-sear">
      <view class="scrolltop">
        <view
          class="section"
          @tap="toSearchPage"
        >
          <image class="search-img" src="@/static/images/icon/search.png" />
          <text class="placeholder">搜索 </text>
        </view>
      </view>
    </view> -->

    <view class="content">
      <!-- swiper -->
      <swiper
        :autoplay="autoplay"
        :indicator-color="indicatorColor"
        :interval="interval"
        :duration="duration"
        :indicator-active-color="indicatorActiveColor"
        :circular="true"
        class="pic-swiper"
        indicator-dots
        previous-margin="20rpx"
        next-margin="20rpx"
      >
        <block
          v-for="(item, index) in indexImgs"
          :key="index"
        >
          <swiper-item class="banner-item">
            <view class="img-box">
              <image
                class="banner"
                :src="item.bannerLogo"
                :data-prodid="item.relation"
                mode="widthFix"
                @tap="toProdPage"
              />
            </view>
          </swiper-item>
        </block>
      </swiper>
      <!-- end swiper -->
      <!-- order -->
      <view class="order">
        <image src="@/static/images/adv.jpg" mode="widthFix" />
        <button class="enter" @tap="onOrder">进入订购<view class="triangle"></view></button>
      </view>
      <!-- end order -->
    </view>
    <view class="updata">
      <!-- 精选推荐 -->
      <view class="hot-sale">
        <view class="title">
          <text>精选推荐</text>
        </view>
        <view class="hotsale-item-cont">
          <block
            v-for="(prod, index) in prodList"
            :key="index"
          >
            <view
              class="prod-items"
              :data-prodid="prod.id"
              :data-prod="prod"
              @tap="toProdPage"
            >
              <view class="hot-imagecont">
                <img class="hotsaleimg" :src="prod.logo" />
              </view>
              <view class="hot-text">
                <view class="hotprod-text">
                  {{ prod.name }}
                </view>
                <view class="prod-text-info">
                  <view class="price">
                    <text class="symbol">
                      ￥
                    </text>
                    <text class="big-num">
                      {{ wxs.parsePrice(prod.price)[0] }}
                    </text>
                    <text class="small-num">
                      .{{ wxs.parsePrice(prod.price)[1] }}
                    </text>
                  </view>
                </view>
              </view>
            </view>
          </block>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
const wxs = number()
const indicatorColor = ref('#d1e5fb')
const indicatorActiveColor = ref('#f06457')
const autoplay = ref(true)
const interval = ref(2000)
const duration = ref(1000)
const indexImgs = ref([])
const prodList = ref([])

onLoad(() => {
  getAllData()
})
onShow(() => {
  // #ifdef MP-WEIXIN
  uni.getSetting({
    success (res) {
      if (!res.authSetting['scope.userInfo']) {
        uni.navigateTo({
          url: '/pages/login/login'
        })
      }
    }
  })
  // #endif
})

const getAllData = () => {
  uniLogin()
  getIndexImgs()
  getProd()
}
const toProdPage = (e) => {
  const { prod, prodid } = e.currentTarget.dataset
  // uni.setStorageSync("goodsData", { ...prod, prodid })
  uni.navigateTo({
    url: '/pages/prod/prod?id=' + prodid
  })
}

/**
 * 跳转搜索页
 */
const toSearchPage = () => {
  uni.navigateTo({
    url: '/pages/search-page/search-page'
  })
}

/**
 * 跳转校服订购专区
 */
const onOrder = () => {
  uni.navigateTo({
    url: '/pages/subscribe/subscribe'
  })
}
onShareAppMessage(() => {
  return {
    title: "元集校服"
  }
})
/**
 * 跳转商品活动页面
 * @param e
 */
const toClassifyPage = (e) => {
  const { sts, id, title } = e.currentTarget.dataset
  let url = '/pages/prod-classify/prod-classify?sts=' + sts
  if (id) {
    url += '&tagid=' + id + '&title=' + title
  }
  uni.navigateTo({
    url
  })
}

/**
 * 静默登录
 */
const uniLogin = () => {
  uni.login({
    provider: 'weixin',

    success: (res) => {
      uni.request({
        url: `https://xinxuenian.yztwlkj.com/base/getOpenIdByCode/${res.code}`,
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/json',
        },
      }).then(({ data }) => {
        uni.setStorageSync('openId', data?.openId)
      })
    },
    fail: (error) => {
      console.error('登录失败：', error);
    }
  })
}
/**
 * 加载轮播图
 */
const getIndexImgs = () => {
  http.request({
    url: '/xcx/bannerList',
    method: 'POST',
    data: {}
  }).then(({ data }) => {
    indexImgs.value = data?.result
  })
}

/**
 * 加载商品列表
 */
const getProd = () => {
  // 加载学校列表
  http.request({
    url: '/xcx/schoolList',
    method: 'POST',
    data: {}
  }).then(({ data}) => {
    const schoolList = data.result ?? []
    let school = {}
    schoolList.map(m => {
      if (m.isTop == 1) {
        school = m
      }
    })
    console.log(school?.id, 123);
    if(school?.id || school?.id == 0){
      http.request({
        url: '/xcx/recommendGoodsList',
        method: 'POST',
        data: { schoolId: school?.id }
      }).then(({ data }) => {
        prodList.value = data?.result
      })
    }
  })
}

</script>

<style scoped lang="scss">
@use './index.scss';
</style>
